﻿#include "CPushButtenA.h"
#include <QPainter>
#include <QMouseEvent>
#include <math.h>
#include <QDebug>

CPushButtenA::CPushButtenA(QWidget *parent)
{
	setParent(parent);
	m_sUp = "上";
	m_sDown = "下";
	m_sLeft = "左";
	m_sRight = "右";
	m_sCenter = "";

	m_bEnableUse = true;//是否允许使用(默认允许)
}

CPushButtenA::~CPushButtenA()
{
}

void CPushButtenA::SetPushButtenText(QString sCenter)
{
	 m_Mutex.lock();
	 m_sCenter = sCenter;
	 m_Mutex.unlock();
}

void CPushButtenA::SetPushButtenText(QString sCenter, QString sUp, QString sDown, QString sLeft, QString sRight)
{
	m_Mutex.lock();
	m_sCenter = sCenter;
	m_sUp = sUp;
	m_sDown = sDown;
	m_sLeft = sLeft;
	m_sRight = sRight;
	m_Mutex.unlock();
}

//设置所有按钮状态
void CPushButtenA::setPushButtonsEnable(bool bState)
{
	if (true == bState)
	{

	}
	else
	{

	}
}

//禁用所有操作按钮
void CPushButtenA::forbiddenAllOperateButtonA()
{
	m_bEnableUse = false;
}

//启用所有操作按钮
void CPushButtenA::enableAllOperateButtonA()
{
	m_bEnableUse = true;
}

void CPushButtenA::mousePressEvent(QMouseEvent *event)
{
	if (false == m_bEnableUse)
	{
		return;
	}

	if (event->button() == Qt::LeftButton)
	{
		QPoint point = event->pos();
		if (isPointInCir(point, drawRect.toRect()))
		{
			if (isPointInCir(point, centerCircularRect.toRect()))
			{
				pressedBtn = pressBtnType::center;
				emit ClieckedCenter();
			}
			else
			{
				QPoint centerPoint = drawRect.toRect().center();
				double angle = atan2(point.y() - centerPoint.y(), point.x() - centerPoint.x()); //两点之间的角度（弧度）
				angle = -angle * (180 / 3.1415926); //0°~180° - -180°~0°
				if (angle < 0.0)
				{
					angle = 360.0 - abs(angle);
				}
				if (angle < 45.0 || angle > 315.0)
				{
					pressedBtn = pressBtnType::right;	
					emit PressedRigth();
				}
				else if (angle >= 45.0 && angle < 135.0)
				{
					pressedBtn = pressBtnType::up;
					emit PressedUp();
				}
				else if (angle >= 135.0 && angle < 225.0)
				{
					pressedBtn = pressBtnType::left;
					emit PressedLeft();
				}
				else if (angle >= 225.0 && angle < 315.0)
				{
					pressedBtn = pressBtnType::down;
					emit PressedDown();
				}
			}
			update();
		}
	}
	return QWidget::mousePressEvent(event);
}

void CPushButtenA::mouseReleaseEvent(QMouseEvent* event)
{
	if (false == m_bEnableUse)
	{
		return;
	}

	if (pressedBtn != pressBtnType::None)
	{
		QPoint point = event->pos();
		if (isPointInCir(point, drawRect.toRect()))
		{
			if (isPointInCir(point, centerCircularRect.toRect()))
			{
				pressedBtn = pressBtnType::center;
				emit ClieckedCenterR();
			}
			else
			{
				QPoint centerPoint = drawRect.toRect().center();
				double angle = atan2(point.y() - centerPoint.y(), point.x() - centerPoint.x()); //两点之间的角度（弧度）
				angle = -angle * (180 / 3.1415926); //0°~180° - -180°~0°
				if (angle < 0.0)
				{
					angle = 360.0 - abs(angle);
				}
				if (angle < 45.0 || angle > 315.0)
				{
					if (pressedBtn == pressBtnType::right)
					emit ReleasedRigth();
				}
				else if (angle >= 45.0 && angle < 135.0)
				{
					emit ReleasedUp();
				}
				else if (angle >= 135.0 && angle < 225.0)
				{
					if(pressedBtn == pressBtnType::left)
					emit ReleasedLeft();
				}
				else if (angle >= 225.0 && angle < 315.0)
				{
					emit ReleasedDown();
				}
			}
		}
		pressedBtn = pressBtnType::None;
		update();
	}
	return QWidget::mouseReleaseEvent(event);
}

QPainterPath CPushButtenA::gradientArc(double startAngle, double angleLength, double arcHeight)
{
	QPainterPath path;
	path.moveTo(drawRect.center());
	path.arcTo(drawRect, startAngle, angleLength);

	QPainterPath subPath;
	subPath.addEllipse(drawRect.adjusted(arcHeight, arcHeight, -arcHeight, -arcHeight));

	// path为扇形 subPath为椭圆
	path -= subPath;
	return path;
}

void CPushButtenA::paintEvent(QPaintEvent *event)
{
	m_Mutex.lock();
	Q_UNUSED(event);
	QPainter painter(this);
	painter.setPen(Qt::NoPen);

	painter.setRenderHint(QPainter::Antialiasing, true);
	painter.setRenderHint(QPainter::SmoothPixmapTransform, true);

	QPoint centerPoint = rect().center();

	painter.save();
	painter.setPen(QPen(QColor(255, 255, 255),1.5));
	painter.setBrush(QColor(87, 163, 176));
	double radius = (std::min(width(), height()) - 10 * 2) / 2;
	drawRect = QRectF(centerPoint.x() - radius, centerPoint.y() - radius, radius * 2, radius * 2);
	double arcHeight = radius / 2;

	fanShaped[0] = gradientArc(45.0, 90.0, arcHeight);//上
	fanShaped[1] = gradientArc(135.0, 90.0, arcHeight);//左
	fanShaped[2] = gradientArc(225.0, 90.0, arcHeight);//下
	fanShaped[3] = gradientArc(315.0, 90.0, arcHeight);//右

	

	for (int i = 0; i < 4; ++i)
	{	
		painter.drawPath(fanShaped[i]);
	}
	painter.restore();

	

	centerCircularRect = QRectF(centerPoint.x() - (radius / 2), centerPoint.y() - (radius / 2), radius, radius).adjusted(2, 2, -2, -2);
	painter.save();
	painter.setPen(Qt::transparent);
	painter.setBrush(QColor(87, 163, 176));
	painter.drawEllipse(centerCircularRect);
	painter.restore();

	//addwxy禁用之后需要绘制背景色灰色
	if (false == m_bEnableUse)
	{
		painter.save();
		QColor colorGray(188, 188, 188);
		painter.setBrush(colorGray);
		painter.setPen(Qt::white);
		painter.drawEllipse(drawRect);
		painter.restore();
	}

	//绘制文字
	//左
	painter.save();
	QRectF textRect = QRectF(drawRect.x(), centerCircularRect.y(), radius / 2, radius);
	QPen p(Qt::SolidLine);
	p.setColor("#FFFFFF");
	p.setWidth(2);
	painter.setPen(p);
	//QFont font = painter.font();
	QFont font("阿里巴巴普惠体 L", 8);
	painter.setFont(font);
	painter.drawText(textRect, Qt::AlignCenter,m_sLeft);

	//上
	textRect = QRectF(centerCircularRect.x(), drawRect.y(), radius, radius / 2);
	painter.drawText(textRect, Qt::AlignCenter, m_sUp);

	//右
	textRect = QRectF(centerCircularRect.topRight().x(), centerCircularRect.topRight().y(), radius / 2, radius);
	painter.drawText(textRect, Qt::AlignCenter,m_sRight);

	//下
	textRect = QRectF(centerCircularRect.bottomLeft().x(), centerCircularRect.bottomLeft().y(), radius, radius / 2);
	painter.drawText(textRect, Qt::AlignCenter, m_sDown);

	painter.drawText(centerCircularRect, Qt::AlignCenter, m_sCenter);
	painter.restore();

	if (pressedBtn != pressBtnType::None)
	{
		painter.save();
		QColor slightlyOpaqueBlack(14, 164, 203, 120);
		painter.setBrush(slightlyOpaqueBlack);
		painter.setPen(Qt::transparent);
		if (pressedBtn == pressBtnType::center)
		{
			painter.drawEllipse(centerCircularRect);
		}
		else
		{
			int index = -1;
			index = static_cast<int>(pressedBtn);
			if (index >= 0)
			{
				painter.drawPath(fanShaped[index]);
			}
		}
		painter.restore();
	}
	
	

	
	//painter.restore();
	m_Mutex.unlock();
}

bool CPushButtenA::isPointInCir(const QPoint &point, const QRect & rect)//判断点是否在圆范围内
{
	const QPoint & centerPoint = rect.center();
	int x = point.x() - centerPoint.x();
	int y = point.y() - centerPoint.y();
	if (sqrt(pow(x, 2) + pow(y, 2)) <= static_cast<double>(rect.width() / 2))
	{
		return true;
	}
	return false;
}
